#include "qconfigdatabase.h"

#include "qsqlitecontrol.h"

#include <QApplication>
#include <QStringList>

QConfigDataBase::QConfigDataBase():
    m_sqlite_control(new QSqliteControl)
{
}

QConfigDataBase::~QConfigDataBase()
{
    delete m_sqlite_control;
}

void QConfigDataBase::open(const QString &path)
{
    m_sqlite_control->open(path+"/config.db");

    QStringList list=m_sqlite_control->get_tables();
    if(!list.contains(DATA_HOLD))
    {
        m_sqlite_control->create_table(DATA_HOLD,QStringList()<<"data_uuid varchar(48) primary key"<<"value varchar(32)");
    }
}

void QConfigDataBase::close()
{
    m_sqlite_control->close();
}

void QConfigDataBase::save_hold_data(const QString &uuid, const QVariant &value)
{
    m_sqlite_control->replace(DATA_HOLD,QStringList()<<uuid<<value.toString());
}

QMap<QString,QString> QConfigDataBase::get_hold_data()
{
    QMap<QString,QString> ret;

    QSqlQuery query=m_sqlite_control->exec(QString("select * from ")+DATA_HOLD);

    while(query.next())
    {
        ret.insert(query.value(0).toString(),query.value(1).toString());
    }
    return ret;
}
